﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.ObjectModel;
using Microsoft.Phone.Controls;
using GeoFinderClientApp.ViewModel;
using GeoFinderClientApp.Model;

namespace GeoFinderClientApp.View
{
    public partial class Settings : PhoneApplicationPage
    {
        ViewModels vm;
        public Settings()
        {
            InitializeComponent();
            vm = new ViewModels();
        }

        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            base.OnNavigatedTo(e);


            // Old instance of the application
            // The user started the application from the Back button.
            if (!StateUtilities.IsLaunching && this.State.ContainsKey("Options"))
            {
                vm = (ViewModels)this.State["Options"];
            }
            // New instance of the application
            // The user started the application from the application list,
            // or there is no saved state available.
            else
            {
                vm.RestoreSavedOptions();
                vm.getAllOptions();
            }


            // There are two different views, but only one view model.
            // So, use LINQ queries to populate the views.

            // Set the data context for the Item view.

            
                PushSettingsControl.DataContext = (from elem in vm.PushOptions select elem.GetCopy()).ToList<PushOption>(); //from Accomplishment in vm.PushOptions select Accomplishment;

                TimeOutSettingsControl.DataContext = (from elem in vm.TimeOutOptions select elem.GetCopy()).ToList<TimeOutsOption>();

                SearchingAreaSettingsControl.DataContext = (from elem in vm.SearchingAreaOptions select elem.GetCopy()).ToList<SearchingAreaOption>();
        }

        protected override void OnNavigatedFrom(System.Windows.Navigation.NavigationEventArgs e)
        {
            base.OnNavigatedFrom(e);

            vm.RestoreSavedOptions();

            if (this.State.ContainsKey("Options"))
            {
                this.State["Options"] = vm;
            }
            else
            {
                this.State.Add("Options", vm);
            }

            StateUtilities.IsLaunching = false;
        }

        private void AppBarSave_Click(object sender, EventArgs e)
        {
            switch (PivotController.SelectedIndex)
            {
                case 0:
                    var mod = new ObservableCollection<PushOption>();
                    foreach (var item in PushSettingsControl.DataContext as List<PushOption>)
                    {
                        mod.Add(item);
                    }
                    vm.PushOptions = mod;
                    vm.SavePushOptions();
                    Web.WebCore.UpdatePushOptions();
                    MessageBox.Show("Finished saving Push Options");
                    break;
                case 1:
                    var to = new ObservableCollection<TimeOutsOption>();
                    foreach (var item in TimeOutSettingsControl.DataContext as List<TimeOutsOption>)
                    {
                        to.Add(item);
                    }
                    vm.TimeOutOptions = to;
                    vm.SaveTimeOutOptions();
                    MessageBox.Show("Finished saving Time out Options");
                    break;
                case 2:
                    var sa = new ObservableCollection<SearchingAreaOption>();
                    foreach (var item in SearchingAreaSettingsControl.DataContext as List<SearchingAreaOption>)
                    {
                        sa.Add(item);
                    }
                    vm.SearchingAreaOptions = sa;
                    vm.SaveSearchingAreaOptions();
                    Web.WebCore.UpdateSearchingAreaOptions();
                    MessageBox.Show("Finished saving Searching area Options");
                    break;
                default: break;
            }
        }

        private void SaveAll_Click(object sender, EventArgs e)
        {
            var push = new ObservableCollection<PushOption>();
            foreach (var item in PushSettingsControl.DataContext as List<PushOption>)
            {
                push.Add(item);
            }
            vm.PushOptions = push;

            var to = new ObservableCollection<TimeOutsOption>();
            foreach (var item in TimeOutSettingsControl.DataContext as List<TimeOutsOption>)
            {
                to.Add(item);
            }
            vm.TimeOutOptions = to;


            var sa = new ObservableCollection<SearchingAreaOption>();
            foreach (var item in SearchingAreaSettingsControl.DataContext as List<SearchingAreaOption>)
            {
                sa.Add(item);
            }
            vm.SearchingAreaOptions = sa;

            vm.SaveAllOptions();

            Web.WebCore.UpdatePushOptions();
            Web.WebCore.UpdateSearchingAreaOptions();

            MessageBox.Show("Finished saving All Options");

            if (NavigationService.CanGoBack)
            {
                NavigationService.GoBack();
            }
            else
            {
                NavigationService.Navigate(new Uri("/View/ApplicationMain.xaml", UriKind.Relative));
            }

        }
        
    }
}